<?php

class EDPLogger
{
	private $logFile;
	private $logPath;
	private $historyPath;
	private $printStackTrace;
	
	public function __construct( $logFile, $logPath, $historyPath, $printStackTrace = 0 )
	{
		$this->logFile = $logFile;
		$this->logPath = $logPath;
		$this->historyPath = $historyPath;
		$this->printStackTrace = $printStackTrace;
	}
	
	public function error( $msg )
	{
		$this->writeLineToFile( $this->logPath . $this->logFile, $msg );
	}
	
	function writeLineToFile($inFileName, $inLine)
	{
		global $page_base;
		
		if( $this->printStackTrace === 0 )
			$inLine = GetCurrentDate() . "  " . $page_base. ' ' . $inLine;
		else {
			$traces = debug_backtrace();
			
			$i = 0;
			$functions = '';
			foreach( $traces as $key => $value )
			{
				if( $i > 10 )
					break;
				$functions .= $value['file'] . ' : ' . $value['function'] . " : " . $value['line'] . "\n" ;
				$i++;
			} 
			
			$inLine = GetCurrentDate() . "  " . $page_base. ' ' . $inLine . "\n stack trace : $functions";
		}
		$handle = fopen($inFileName, "a+");
	    fputs($handle,$inLine . "\n");
		fclose($handle);
		
		// check the file size if it is over a meg, move it to the history folder
		if (filesize($inFileName)  > (1*1024*1024))
		{
			$historyFile = date("Y-m-d-h-i") . '_error.log';
			copy( $inFileName, $this->historyPath . $historyFile );
			unlink( $inFileName );
		}
	
	}

}

?>